package com.linkelee.properties;

import com.linkelee.annotation.NotifyListener;
import lombok.Builder;
import lombok.Data;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import java.lang.annotation.Annotation;

/**
 * 消费者创建配置信息
 * @author lsl
 * @since 2019-02-21
 */

@Data
@Builder
public class ConsumerBuildProperties {



    private String exchangeType;

    private String exchangeName;

    private String queueName;

    private String routingKey;

    private boolean autoDeleted;

    private boolean durable;

    private boolean autoAck;


    public static ConsumerBuildProperties genProperties(ChannelAwareMessageListener listener, String beanName) throws Exception {
        Annotation annotation = listener.getClass().getAnnotation(NotifyListener.class);
        if (annotation == null) {
            return ConsumerBuildProperties.builder()
                    .exchangeType(ExchangeTypes.DIRECT)
                    .routingKey(RpcMqElementKeyBuilder.buildRoutingKey(beanName))
                    .queueName(RpcMqElementKeyBuilder.buildQueueName(beanName))
                    .autoAck(true)
                    .autoDeleted(false)
                    .durable(false)
                    .build();
        } else {
            return ConsumerBuildProperties.builder()
                    .exchangeName(RpcMqElementKeyBuilder.buildExchangeName(beanName))
                    .exchangeType(((NotifyListener) annotation).exchangeType())
                    .routingKey(RpcMqElementKeyBuilder.buildRoutingKey(beanName))
                    .queueName(RpcMqElementKeyBuilder.buildQueueName(beanName))
                    .autoAck(((NotifyListener) annotation).autoAck())
                    .autoDeleted(((NotifyListener) annotation).autoDeleted())
                    .durable(((NotifyListener) annotation).durable())
                    .build();
        }
    }





}
